package com.huiyeji.xlsx;

import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XLSXRowContext {

    private final Map<String,DataColumn> p = new HashMap<String, DataColumn>();

    private final List<DataColumn> l;

    private final c c;

    private final int n;

    private final Map<String,Container> s = new HashMap<String, Container>();

    public XLSXRowContext(List<DataColumn> l, List<Container> s, c c, int n){
        this.l = l;
        this.c = c;
        this.n = n + 1;

        //初始化数据容器
        if (s != null && s.size() > 0){
            for (Container sc : s) {
                if (StringUtils.hasLength(sc.getName())){
                    this.s.put(sc.getName(),sc);
                }
            }
        }

        //初始化列名与列对象映射关系
        for (DataColumn lc : l) {
            p.put(lc.getColumnName(),lc);
        }
    }

    public Container getContainer(String containerName) {
        return s.get(containerName);
    }

    public DataColumn getColumn(String columnName) {
        return p.get(columnName);
    }

    public String getColumnValue(String columnName){
        DataColumn column = getColumn(columnName);
        return column == null ? null : column.getColumnValue();
    }

    public List<DataColumn> getColumns(){
        return l;
    }

    public int getRowNumber() {
        return n;
    }

    /**
     * 在行末尾追加写入文本内容
     * @param content 文本内容
     * @param contentType 文本类型
     */
    public void appendWrite(String content,ContentType contentType){
        c.writeBack(content,contentType.getIndexedColors().getIndex());
    }
}
